import { reactive, ref } from 'vue'
import * as $http from '@/utlis/interface/Wode'
import { useStore } from '@/Pinia/index'
import { nowDate } from "@/utlis/time"
interface User {
  realNameStatus: string | null
  nickname: string | null
  avatar: string | null
}
export default (proxy: any) => {
  let userInfo = reactive<User>({
    realNameStatus: null,
    nickname: null,
    avatar: null,
  })
  //获取个人资料
  const getMyInfo = () => {
    $http.userInfoMsg(proxy).then((res: any) => {
      if (res.code == 0) {
        userInfo.avatar = res.data.avatar
        userInfo.nickname = res.data.nickname
        userInfo.realNameStatus = res.data.realNameStatus
      }
    })
  }
  const nowsAVATRA = useStore()
  //上传头像
  const afterRead = (event: any) => {
    uni.chooseImage({
      sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
      success: ((chooseImageRes: any) => {
        userInfo.avatar = chooseImageRes.tempFilePaths[0]
        uni.showLoading({
          title: '正在上传',
        })

        uni.uploadFile({
          url: `https://api.waimaishi.cn/app-api/common/AppUpload`,
          name: 'file', //二进制文件
          filePath: chooseImageRes.tempFilePaths[0],
          header: {
            Authorization: uni.getStorageSync('Authorization'),
          },
          formData: {
            pathPrefix: 'rider' + nowDate()
          },
          success: (uploadFileRes: any) => {
            uni.hideLoading()
            let imgData = JSON.parse(uploadFileRes.data)
            if (imgData.code == 0) {
              userInfo.avatar = imgData.data
              nowsAVATRA.infoData.userAvatar = imgData.data
              $http.uploadAvatar(userInfo, proxy).then((res: any) => {
              })
              uni.hideLoading()
              proxy.$API(proxy).Toast('上传成功', 'success')
            } else {
              uni.hideLoading()
              proxy.$API(proxy).Toast(imgData.msg, 'error')
            }
          },
          fail: (err) => {
            uni.hideLoading()
            proxy.$API(proxy).Toast('上传上传失败', 'error')
          },
        })

      })
    })


  }
  return {
    userInfo,
    getMyInfo,
    afterRead,
  }
}
